var t = getApp(), o = [], s = 0, a = 1;

Page({
    data: {
        language: "",
        imageURL: "https://img.alicdn.com/tfscom/i2/256691439/TB2wjP9bXgkyKJjSspfXXcj1XXa_!!256691439.jpg_180x180xzq90.jpg_.webp",
        countDownList: [],
        actEndTimeList: [],
        groupDetail: {},
        goodsDetail: {},
        has_many_success_member: "",
        has_many_success_member_count: "",
        member_num: "",
        info: {
            has_one_group: {
                has_one_goods: {
                    thumb: "",
                    market_price: ""
                }
            },
            has_one_level: {
                group_price: ""
            }
        },
        id: "",
        showOpenGroup: !1,
        goodsInfo: {},
        goodsDescription: "请选择",
        popPrice: 0,
        level_id: 0,
        goodsCount: 1,
        goodsId: "",
        activityId: "",
        isOpen: 0,
        status: 0,
        action: 0,
        activityTest: "",
        btnTest: "",
        btnStatus: !1,
        option_level_id: "",
        shareType: "group"
    },
    onLoad: function(i) {
        i.mid && t._setMid(i.mid), i.plu && (this.data.shareType = "ywmGroup"), console.log(i, "参数"), 
        o = [], s = 0, a = 1, this._getData();
    },
    onReady: function() {
        var t = wx.getStorageSync("langIndex");
        this.setData({
            language: t.en
        });
    },
    onShow: function() {
        wx.showShareMenu({
            withShareTicket: !1,
            menus: [ "shareAppMessage", "shareTimeline" ]
        });
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareTimeline: function() {
        var t = wx.getStorageSync("yz_uid"), o = "";
        t && (o = t);
        var s = "id=".concat(this.options.id, "&mid=").concat(o);
        return this.options.store_id && (s = "id=".concat(this.options.id, "&store_id=").concat(this.options.store_id, "&mid=").concat(o)), 
        this.options.plu && "ywm" == this.options.plu && (s += "&plu=ywm"), {
            query: s
        };
    },
    onShareAppMessage: function(t) {
        var o = wx.getStorageSync("yz_uid"), s = "";
        o && (s = o);
        var a = "/packageB/member/group/GroupDetail/GroupDetail?id=" + this.options.id + "&mid=" + s;
        return this.options.store_id && (a = "/packageB/member/group/GroupDetail/GroupDetail?id=" + this.options.id + "&store_id=" + this.options.store_id + "&mid=" + s), 
        this.options.plu && "ywm" == this.options.plu && (a += "&plu=ywm"), {
            title: this.data.info.has_one_group.desc_title ? this.data.info.has_one_group.desc_title : this.data.info.has_one_group.share_title,
            path: a
        };
    },
    tapOverlay: function() {
        this.setData({
            showOpenGroup: !1
        });
    },
    _getData: function() {
        var o = this, s = "fight-groups";
        this.options.plu && "ywm" == this.options.plu && (s = "ywm-fight-groups");
        var a = t.getNetAddresss("plugin.".concat(s, ".frontend.controllers.team.index"));
        a += "&team_id=" + this.options.id, t._getNetWork({
            url: a,
            success: function(t) {
                var s = t.data;
                if (1 == s.result) {
                    console.log(s.data, "数据"), o.setData({
                        info: s.data,
                        goodsInfo: s.data.has_one_group.has_one_goods,
                        level_id: s.data.has_one_level.id,
                        goodsId: s.data.has_one_group.goods_id,
                        isOpen: s.data.has_one_group.status,
                        status: s.data.status,
                        action: s.data.action,
                        activityId: s.data.has_one_group.id,
                        has_many_success_member: s.data.has_many_success_member,
                        has_many_success_member_count: s.data.has_many_success_member_count,
                        member_num: s.data.has_one_level.member_num
                    }), console.log(o.data.isOpen, "判断是否显示");
                    var a = [], i = 1e3 * s.data.end_time;
                    i = new Date(i), console.log(i, "结束时间"), a.push(i), o.setData({
                        actEndTimeList: a
                    }), o.countDown(), o.setStatusBtn(), o.initPopView(), 1 != o.data.goodsInfo.has_option && o.selectGroup();
                } else wx.showToast({
                    icon: "none",
                    title: s.msg,
                    duration: 1500
                });
            }
        });
    },
    initPopView: function() {
        if (console.log(this.data.goodsInfo, "sssssssssssssss"), this.data.goodsInfo && 1 == this.data.goodsInfo.has_option) {
            this.setData({
                popPrice: this.data.info.has_one_group.min_price + "起",
                popStock: this.data.goodsInfo.stock,
                goodsDescription: "选择"
            });
            for (var t = "请选择", o = 0; o < this.data.goodsInfo.has_many_specs.length; o++) t += " " + this.data.goodsInfo.has_many_specs[o].title;
            console.log(this.data.goodsDescription, "详情"), this.setData({
                goodsDescription: t
            });
        } else this.setData({
            popPrice: this.data.info.has_one_group.min_price + "起----",
            popStock: this.data.info.has_one_group.has_one_goods.stock
        }), a = 0 == this.data.info.has_one_group.has_many_options[0].buy_limit ? this.data.popStock : this.data.info.has_one_group.has_many_options[0].buy_limit;
    },
    setStatusBtn: function() {
        if (0 == this.data.isOpen) this.setData({
            activityTest: "当前活动未开始",
            btnTest: "未开始",
            btnStatus: !0
        }); else if (1 == this.data.isOpen) 0 == this.data.status ? 1 == this.data.action ? this.setData({
            activityTest: "支付成功后拼团活动开始！",
            btnTest: "去支付"
        }) : 4 == this.data.action && this.setData({
            activityTest: "活动正在进行中",
            btnTest: "我要开团"
        }) : 1 == this.data.status ? 2 == this.data.action ? (this.setData({
            btnTest: "邀请好友"
        }), wx.showModal({
            title: "提示",
            content: "请点击右上角进行分享",
            success: function(t) {}
        })) : 1 == this.data.action ? this.setData({
            btnTest: "去支付"
        }) : 3 == this.data.action && this.setData({
            btnTest: "我要参团"
        }) : 2 == this.data.status && 4 == this.data.action ? this.setData({
            activityTest: "该团已拼团成功！",
            btnTest: "我要开团"
        }) : 3 == this.data.status && 4 == this.data.action && this.setData({
            activityTest: "未达到拼团人数，拼团失败!",
            btnTest: "我要开团"
        }); else {
            if (2 != this.data.isOpen) return console.log("遇到未知的活动状态，response.data.has_one_group.status"), 
            !1;
            this.setData({
                activityTest: "很抱歉，活动已结束！",
                btnTest: "已结束",
                btnStatus: !0
            });
        }
    },
    tapGoteam: function() {
        if (this.data.btnStatus) return !1;
        4 == this.data.action ? wx.navigateTo({
            url: "/packageA/detail_v2/detail_v2?id=" + this.data.activityId + "/" + this.options.store_id + "&name=" + this.data.shareType
        }) : 3 == this.data.action ? this.setData({
            showOpenGroup: !0
        }) : 2 == this.data.action ? console.log("分享啊啊啊") : 1 == this.data.action && wx.navigateTo({
            url: "/packageD/buy/orderPay/orderPay?status=3&order_id=" + this.data.info.order_id + "&name=" + this.data.shareType
        });
    },
    selectSpecs: function(t) {
        var o = t.target.dataset.item || t.currentTarget.dataset.item, s = t.target.dataset.index || t.currentTarget.dataset.index, a = this.data.goodsInfo;
        a.has_many_specs[s].description = o.title, this.setData({
            goodsInfo: a
        }), this.manageSpecs(o), this.setGoodsSpecs(o), this.setGoodsSpecsChangeInfo(), 
        this.getMaxCount();
    },
    getMaxCount: function() {
        o.length == this.data.goodsInfo.has_many_specs.length && (0 == a && this.setData({
            goodsCount: 0
        }), this.data.goodsCount > a && this.setData({
            goodsCount: a
        }));
    },
    setGoodsSpecsChangeInfo: function() {
        if (o.sort(function(t, o) {
            return t.id - o.id;
        }), o.length == this.data.goodsInfo.has_many_specs.length) {
            for (var i = "", e = 0; e < o.length; e++) i += o[e].id + "_";
            i = i.substring(0, i.length - 1);
            for (var n = 0; n < this.data.goodsInfo.has_many_options.length; n++) if (i == this.setGoodsSpecsBySort(this.data.goodsInfo.has_many_options[n].specs)) {
                this.setData({
                    popPrice: this.data.goodsInfo.has_many_options[n].product_price,
                    popThumb: t._isTextEmpty(this.data.goodsInfo.has_many_options[n].thumb) ? this.data.goodsInfo.thumb : this.data.goodsInfo.has_many_options[n].thumb,
                    popStock: this.data.info.has_one_group.has_many_options[n].stock
                }), s = this.data.goodsInfo.has_many_options[n].id, (a = this.data.goodsInfo.has_many_options[n].stock) > 0 && this.setData({
                    goodsCount: 1
                }), this.setGroupInfo(s);
                break;
            }
            console.log("options", s);
        }
    },
    setGoodsSpecsBySort: function(t) {
        var o = t.split("_");
        o.sort(function(t, o) {
            return t - o;
        });
        for (var s = "", a = 0; a < o.length; a++) s += o[a] + "_";
        return s = s.substring(0, s.length - 1);
    },
    manageSpecs: function(t) {
        var s = {};
        if (s.id = t.id, s.specid = t.specid, s.title = t.title, o.length > 0) {
            for (var a = 0; a < o.length; a++) o[a].specid == s.specid && o.splice(a, 1);
            o.push(s);
        } else o.push(s);
        if (o.length == this.data.goodsInfo.has_many_specs.length) {
            for (var i = [], e = 0; e < this.data.goodsInfo.has_many_specs.length; e++) for (var n = 0; n < o.length; n++) if (this.data.goodsInfo.has_many_specs[e].id == o[n].specid) {
                i.push(o[n]);
                break;
            }
            o = i;
        }
        this.setGoodsDescription();
    },
    setGoodsDescription: function() {
        var t = "";
        if (o.length == this.data.goodsInfo.has_many_specs.length) {
            t = "已选择 ";
            for (var s = 0; s < o.length; s++) t += o[s].title + " ";
            this.setData({
                goodsDescription: t
            });
        } else {
            t = "请选择 ";
            for (var a = 0; a < this.data.goodsInfo.has_many_specs.length; a++) for (var i = 0; i < o.length; i++) if (this.data.goodsInfo.has_many_specs[a].id != o[i].specid) {
                t += this.data.goodsInfo.has_many_specs[a].title + " ";
                break;
            }
            this.setData({
                goodsDescription: t
            });
        }
        console.log(this.data.goodsDescription, "参数");
    },
    setGoodsSpecs: function(t) {
        for (var o = 0; o < this.data.goodsInfo.has_many_specs.length; o++) t.specid != this.data.goodsInfo.has_many_specs[o].id && this.setGoodsSpecsStatus(this.data.goodsInfo.has_many_specs[o].has_many_specs_item, t.id);
    },
    setGoodsSpecsStatus: function(t, o) {
        for (var s = [], a = 0; a < this.data.goodsInfo.has_many_options.length; a++) for (var i = this.data.goodsInfo.has_many_options[a].specs.split("_"), e = 0; e < i.length; e++) if (i[e] == o) return void s.push(this.data.goodsInfo.has_many_options[a]);
        for (var n = 0; n < s.length; n++) for (var d = s[n].specs.split("_"), h = 0; h < d.length; h++) if (d[h] != o && 0 == s[n].stock) {
            for (var r = 0; r < t.length; r++) if (d[h] == t[r].id) return void (t[r].c = !0);
        } else if (d[h] != o && s[n].stock > 0) for (var p = 0; p < t.length; p++) if (d[h] == t[p].id) return void (t[p].c = !1);
        console.log(s);
    },
    setGroupInfo: function(t) {
        for (var o = 0; o < this.data.info.has_one_group.has_many_options.length; o++) {
            if (this.data.info.has_one_group.has_many_options[o].option_id == s) return this.setData({
                selectGroupInfo: this.data.info.has_one_group.has_many_options[o].has_many_option_level,
                popStock: this.data.info.has_one_group.has_many_options[o].stock
            }), a = 0 == this.data.info.has_one_group.has_many_options[o].buy_limit ? this.data.popStock : this.data.info.has_one_group.has_many_options[o].buy_limit, 
            void this.selectGroup();
        }
    },
    selectGroup: function(t) {
        var o = this, s = [];
        if (1 == this.data.goodsInfo.has_option) s = this.data.selectGroupInfo.filter(function(t) {
            if (t.level_id == o.data.level_id) return t;
        }), console.log(s, "98655465445"), this.setData({
            popPrice: s[0].group_price
        }), this.setData({
            option_level_id: s[0].id
        }); else {
            var a = this.data.info.has_one_group.has_many_options[0].has_many_option_level;
            this.setData({
                selectGroupInfo: a
            }), console.log(this.data.selectGroupInfo, "对象"), s = this.data.selectGroupInfo.filter(function(t) {
                if (t.level_id == o.data.level_id) return t.group_price;
            }), console.log(s, "东西"), this.setData({
                popPrice: s[0].group_price
            }), this.setData({
                option_level_id: s[0].id
            });
        }
    },
    addGoods: function() {
        if (console.log("增加", this.popStock, a), 1 == this.data.goodsInfo.has_option && t._isTextEmpty(s)) return console.log("请选择商品规格！"), 
        void wx.showToast({
            title: "请选择商品规格！",
            icon: "none"
        });
        if (this.data.popStock) {
            if (this.data.goodsCount > this.data.popStock) return wx.showToast({
                title: "库存不足",
                icon: "none"
            }), void this.setData({
                goodsCount: this.data.popStock
            });
            if (0 != a && this.data.goodsCount >= a) return wx.showToast({
                title: "抱歉，该商品限购".concat(a, "件"),
                icon: "none"
            }), void this.setData({
                goodsCount: a
            });
            var o = this.data.goodsCount;
            o++, this.setData({
                goodsCount: o
            });
        } else wx.showToast({
            title: "库存不足",
            icon: "none"
        });
    },
    reduceGoods: function() {
        if (1 != this.data.goodsCount && 0 != this.data.goodsCount) {
            var t = this.data.goodsCount;
            t--, this.setData({
                goodsCount: t
            });
        }
    },
    changeCount: function() {
        if (this.data.popStock) return this.data.goodsCount > this.data.popStock ? (wx.showToast({
            title: "库存不足",
            icon: "none"
        }), void this.setData({
            goodsCount: this.data.popStock
        })) : 0 != a && this.data.goodsCount > a ? (wx.showToast({
            title: "数量超出范围",
            icon: "none"
        }), void this.setData({
            goodsCount: a
        })) : this.data.goodsCount <= 0 ? (wx.showToast({
            title: "商品数量不能小于0",
            icon: "none"
        }), void this.setData({
            goodsCount: 1
        })) : void 0;
        wx.showToast({
            title: "库存不足",
            icon: "none"
        });
    },
    joinTeam: function() {
        var o = this;
        if (1 == this.data.goodsInfo.has_option && t._isTextEmpty(s)) return console.log("请选择商品规格！"), 
        void wx.showToast({
            title: "请选择商品规格！",
            icon: "none"
        });
        if (this.data.popStock) {
            if (this.data.goodsCount > this.data.popStock) return wx.showToast({
                title: "库存不足",
                icon: "none"
            }), void this.setData({
                goodsCount: this.data.popStock
            });
            if (0 != a && this.data.goodsCount > a) return wx.showToast({
                title: "数量超出范围",
                icon: "none"
            }), void this.setData({
                goodsCount: a
            });
            if (this.data.goodsCount <= 0) return wx.showToast({
                title: "商品数量不能小于0",
                icon: "none"
            }), void this.setData({
                goodsCount: 1
            });
            0 == this.data.goodsInfo.has_option && (s = 0);
            var i = [];
            i.push(this.data.goodsId);
            var e = t.getNetAddresss("from.div-from.isDisplay");
            e += "&goods_ids=" + JSON.stringify(i), t._getNetWork({
                showToastIn: !1,
                url: e,
                success: function(t) {
                    var a = t.data;
                    if (1 == a.result) if (a.data.status && !a.data.member_status) wx.showModal({
                        title: "提示",
                        content: "购买此商品,请补充您的个人信息",
                        success: function(t) {
                            t.confirm ? wx.navigateTo({
                                url: "/packageB/member/address/myinfo/myinfo?tag=delta"
                            }) : t.cancel && console.log("用户点击取消");
                        }
                    }); else {
                        var i = 0;
                        o.options.store_id && null != o.options.store_id && 0 != o.options.store_id && (i = o.options.store_id);
                        var e = "/packageD/buy/myOrder_v2/myOrder_v2?tag=-2&goodsId=" + o.data.goodsId + "&optionsId=" + s + "&total=" + o.data.goodsCount + "&team_id=" + o.options.id + "&option_level_id=" + o.data.option_level_id + "&groupStoreID=" + i;
                        o.options.plu && "ywm" == o.options.plu && (e += "&plu=ywm"), wx.navigateTo({
                            url: e
                        });
                    } else wx.showToast({
                        icon: "none",
                        title: a.msg,
                        duration: 1500
                    });
                },
                fail: function(t) {}
            });
        } else wx.showToast({
            title: "库存不足",
            icon: "none"
        });
    },
    timeFormat: function(t) {
        return t < 10 ? "0" + t : t;
    },
    countDown: function() {
        var t = this, o = new Date().getTime(), s = this.data.actEndTimeList, a = [];
        s.forEach(function(s) {
            var i = new Date(s).getTime(), e = null;
            if (i - o > 0) {
                var n = (i - o) / 1e3, d = parseInt(n / 86400), h = parseInt(n % 86400 / 3600), r = parseInt(n % 86400 % 3600 / 60), p = parseInt(n % 86400 % 3600 % 60);
                e = {
                    day: t.timeFormat(d),
                    hou: t.timeFormat(h),
                    min: t.timeFormat(r),
                    sec: t.timeFormat(p)
                }, t.setData({
                    istime: !1
                });
            } else e = {
                day: "00",
                hou: "00",
                min: "00",
                sec: "00"
            }, t.setData({
                istime: !0
            });
            a.push(e);
        }), this.setData({
            countDownList: a
        }), setTimeout(this.countDown, 1e3);
    },
    goHome: function() {
        wx.reLaunch({
            url: "/packageG/index/index"
        });
    },
    gotoGoods: function() {
        wx.navigateTo({
            url: "/packageA/detail_v2/detail_v2?id=" + this.data.activityId + "/" + this.options.store_id + "&name=" + this.data.shareType
        });
    }
});